package io.wins.vertx.web;

import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.handler.TimeoutHandler;

public class ServerProperties {

    private long requestTimeout = TimeoutHandler.DEFAULT_TIMEOUT;
    private boolean validateRequest = true;
    private int port = 8080;
    private String uploadFilePath = "uploads/";
    private String AbsUploadPath;
    private boolean enabledDev = false;
    private JsonObject jwtAuthConfig;


    static ServerProperties fromJson(final JsonObject json) {
        final ServerProperties serverProperties = new ServerProperties();

        if (json.containsKey("port")) {
            serverProperties.port = json.getInteger("port");
        }

        if (json.containsKey("request")) {
            final JsonObject request = json.getJsonObject("request");
            if (request.containsKey("timeout")) {
                serverProperties.requestTimeout = request.getLong("timeout");
            }
            if (request.containsKey("validate")) {
                serverProperties.validateRequest = request.getBoolean("validate");
            }
        }
        if (json.containsKey("uploadFilePath")) {
            serverProperties.uploadFilePath += json.getString("uploadFilePath");
        }
        serverProperties.AbsUploadPath = serverProperties.getClass().getClassLoader().getResource("webapp/").getPath() + serverProperties.uploadFilePath;
        if (json.containsKey("dev")) {
            serverProperties.enabledDev = json.getBoolean("dev");
        }
        serverProperties.jwtAuthConfig = new JsonObject()
            .put("keyStore", new JsonObject()
                .put("type", "jceks")
                .put("path", "keystore.jceks")
                .put("password", "secret")
            );
        return serverProperties;
    }

    public long getRequestTimeout() {
        return this.requestTimeout;
    }

    public boolean isValidateRequest() {
        return this.validateRequest;
    }

    public int getPort() {
        return this.port;
    }

    public boolean isEnabledDev() {
        return this.enabledDev;
    }

    public JsonObject getJwtAuthConfig() {
        return this.jwtAuthConfig;
    }

    public String getUploadFilePath() {
        return this.uploadFilePath;
    }

    public String getAbsUploadPath() {
        return this.AbsUploadPath;
    }
}
